MV_CC_SaveImageEx2

MV_CC_SaveImageEx2

Convert the original image data to picture and save the pictures to specific memory, supports setting JPEG encoding quality.

API Definition

int MV_CC_SaveImageEx2(
  void*                     handle,
  MV_SAVE_IMAGE_PARAM_EX    *pSaveParam
);

Parameters

handle

[IN] Device handle, which is returned by MV_CC_CreateHandle or MV_CC_CreateHandleWithoutLog.

pSaveParam

[IN] [OUT] Input and output parameters of picture data, see the structure MV_SAVE_IMAGE_PARAM_EX for details.

Return Value

Return MV_OK(0) on success, and return Error Code on failure.

Remarks

  • Once there is image data, you can call this API to convert the data.

  • You can also call MV_CC_GetOneFrameTimeout or MV_CC_RegisterImageCallBackEx or MV_CC_GetImageBuffer to get one image frame and set the callback function, and then call this API to convert the format.

  • Comparing with the previous API MV_CC_SaveImageEx, this API added the parameter handle to ensure the unity with other API.

The following sample code is for reference only.

#include "MvCameraControl.h"
void main()
{
    int nRet = -1;
    void* m_handle = NULL;

    //Enumerate all devices of specific transport protocol in the subnet
     unsigned int nTLayerType = MV_GIGE_DEVICE | MV_USB_DEVICE;
    MV_CC_DEVICE_INFO_LIST m_stDevList = {0};
    int nRet = MV_CC_EnumDevices(nTLayerType, &m_stDevList);
    if (MV_OK != nRet)
    {
        printf("error: EnumDevices fail [%x]\n", nRet);
        return;
    }

    int i = 0;
    if (m_stDevList.nDeviceNum == 0)
    {
        printf("no camera found!\n");
        return;
    }

    //Select the first found online device and create handle
    int nDeviceIndex = 0;

    MV_CC_DEVICE_INFO m_stDevInfo = {0};
    memcpy(&m_stDevInfo, m_stDevList.pDeviceInfo[nDeviceIndex], sizeof(MV_CC_DEVICE_INFO));

    nRet = MV_CC_CreateHandle(&m_handle, &m_stDevInfo);

    if (MV_OK != nRet)
    {
        printf("error: CreateHandle fail [%x]\n", nRet);
        return;
    }

    //Connect to device
     nRet = MV_CC_OpenDevice(m_handle, nAccessMode, nSwitchoverKey);
    if (MV_OK != nRet)
    {
        printf("error: OpenDevice fail [%x]\n", nRet);
        return;
    }
    //Other process...

     //Start image acquisition
     nRet = MV_CC_StartGrabbing(m_handle);
    if (MV_OK != nRet)
    {
        printf("error: StartGrabbing fail [%x]\n", nRet);
        return;
    }

    //Get the size of one data frame
     MVCC_INTVALUE stIntvalue = {0};
    nRet = MV_CC_GetIntValue(m_handle, "PayloadSize", &stIntvalue);
    if (nRet != MV_OK)
    {
        printf("Get PayloadSize failed! nRet [%x]\n", nRet);
        return;
    }
    int nBufSize = stIntvalue.nCurValue + 2048; //One data frame size + reserved bytes

    unsigned int    nTestFrameSize = 0;
    unsigned char*  pFrameBuf = NULL;
    pFrameBuf = (unsigned char*)malloc(nBufSize);

    MV_FRAME_OUT_INFO_EX stInfo;
    memset(&stInfo, 0, sizeof(MV_FRAME_OUT_INFO_EX));

    //The upper-layer program should control the frequency of calling this API according to the frame rate
     //For actual application, you'd better create another thread for collecting and processing picture frame
     while(1)
    {
        if (nTestFrameSize > 99) 
        {
            break;
        }
        nRet = MV_CC_GetOneFrameTimeout(m_handle,pFrameBuf, nBufSize, &stInfo, 1000);
        if (MV_OK != nRet)
        {
            Sleep(10);
        }
        else
        {
            //Input and output parameters of picture data
            MV_SAVE_IMAGE_PARAM_EX stParam;
                              
            //Source data
            stParam.pData         = pFrameBuf;                //Original image data
            stParam.nDataLen      = stFrameInfo.nFrameLen;    //Size of original image data
            stParam.enPixelType   = stFrameInfo.enPixelType;  //Pixel format of original image data
            stParam.nWidth        = stFrameInfo.nWidth;       //Image width
            stParam.nHeight       = stFrameInfo.nHeight;      //Image height
            stParam.nJpgQuality   = 70;						  //JPEG encoding quality

            //Target data
            stParam.enImageType   = MV_Image_Jpeg;            //Image type to be saved, convert to JPEG format
            stParam.nBufferSize   = nBufSize;                 //Storage node size
            unsigned char* pImage = (unsigned char*)malloc(nBufSize);
           stParam.pImageBuffer  = pImage;                   //Buffer for output data, this buffer is also used for storing converted picture data

            nRet = MV_CC_SaveImageEx2(m_handle, &stParam);
            if(MV_OK != nRet)
            {
                break;
            }

            //Save the converted picture data as file
             FILE* fp = fopen("image", "wb");
            fwrite(pImage, 1, stParam.nImageLen, fp);
            fclose(fp);
            free(pImage);

            //Process other image data...
            nTestFrameSize++;
        }
    }
    free(pFrameBuf);

    //Other process...

    //Stop image acquisition
     nRet = MV_CC_StopGrabbing(m_handle);
    if (MV_OK != nRet)
    {
        printf("error: StopGrabbing fail [%x]\n", nRet);
        return;
    }

    //Close camera
     nRet = MV_CC_CloseDevice(m_handle);
    if (MV_OK != nRet)
    {
        printf("error: CloseDevice fail [%x]\n", nRet);
        return;
    }

    //Destroy handle and release resources
     nRet = MV_CC_DestroyHandle(m_handle);
    if (MV_OK != nRet)
    {
        printf("error: DestroyHandle fail [%x]\n", nRet);
        return;
    }    
}

MV_CC_SaveImageEx2

Convert the original image data to picture and save the pictures to specific memory, supports setting JPEG encoding quality.

API Definition

int MV_CC_SaveImageEx2(
  void*                     handle,
  MV_SAVE_IMAGE_PARAM_EX    *pSaveParam
);

Parameters

handle

[IN] Device handle, which is returned by MV_CC_CreateHandle or MV_CC_CreateHandleWithoutLog.

pSaveParam

[IN] [OUT] Input and output parameters of picture data, see the structure MV_SAVE_IMAGE_PARAM_EX for details.

Return Value

Return MV_OK(0) on success, and return Error Code on failure.

Remarks

  • Once there is image data, you can call this API to convert the data.

  • You can also call MV_CC_GetOneFrameTimeout or MV_CC_RegisterImageCallBackEx or MV_CC_GetImageBuffer to get one image frame and set the callback function, and then call this API to convert the format.

  • Comparing with the previous API MV_CC_SaveImageEx, this API added the parameter handle to ensure the unity with other API.

The following sample code is for reference only.

#include "MvCameraControl.h"
void main()
{
    int nRet = -1;
    void* m_handle = NULL;

    //Enumerate all devices of specific transport protocol in the subnet
     unsigned int nTLayerType = MV_GIGE_DEVICE | MV_USB_DEVICE;
    MV_CC_DEVICE_INFO_LIST m_stDevList = {0};
    int nRet = MV_CC_EnumDevices(nTLayerType, &m_stDevList);
    if (MV_OK != nRet)
    {
        printf("error: EnumDevices fail [%x]\n", nRet);
        return;
    }

    int i = 0;
    if (m_stDevList.nDeviceNum == 0)
    {
        printf("no camera found!\n");
        return;
    }

    //Select the first found online device and create handle
    int nDeviceIndex = 0;

    MV_CC_DEVICE_INFO m_stDevInfo = {0};
    memcpy(&m_stDevInfo, m_stDevList.pDeviceInfo[nDeviceIndex], sizeof(MV_CC_DEVICE_INFO));

    nRet = MV_CC_CreateHandle(&m_handle, &m_stDevInfo);

    if (MV_OK != nRet)
    {
        printf("error: CreateHandle fail [%x]\n", nRet);
        return;
    }

    //Connect to device
     nRet = MV_CC_OpenDevice(m_handle, nAccessMode, nSwitchoverKey);
    if (MV_OK != nRet)
    {
        printf("error: OpenDevice fail [%x]\n", nRet);
        return;
    }
    //Other process...

     //Start image acquisition
     nRet = MV_CC_StartGrabbing(m_handle);
    if (MV_OK != nRet)
    {
        printf("error: StartGrabbing fail [%x]\n", nRet);
        return;
    }

    //Get the size of one data frame
     MVCC_INTVALUE stIntvalue = {0};
    nRet = MV_CC_GetIntValue(m_handle, "PayloadSize", &stIntvalue);
    if (nRet != MV_OK)
    {
        printf("Get PayloadSize failed! nRet [%x]\n", nRet);
        return;
    }
    int nBufSize = stIntvalue.nCurValue + 2048; //One data frame size + reserved bytes

    unsigned int    nTestFrameSize = 0;
    unsigned char*  pFrameBuf = NULL;
    pFrameBuf = (unsigned char*)malloc(nBufSize);

    MV_FRAME_OUT_INFO_EX stInfo;
    memset(&stInfo, 0, sizeof(MV_FRAME_OUT_INFO_EX));

    //The upper-layer program should control the frequency of calling this API according to the frame rate
     //For actual application, you'd better create another thread for collecting and processing picture frame
     while(1)
    {
        if (nTestFrameSize > 99) 
        {
            break;
        }
        nRet = MV_CC_GetOneFrameTimeout(m_handle,pFrameBuf, nBufSize, &stInfo, 1000);
        if (MV_OK != nRet)
        {
            Sleep(10);
        }
        else
        {
            //Input and output parameters of picture data
            MV_SAVE_IMAGE_PARAM_EX stParam;
                              
            //Source data
            stParam.pData         = pFrameBuf;                //Original image data
            stParam.nDataLen      = stFrameInfo.nFrameLen;    //Size of original image data
            stParam.enPixelType   = stFrameInfo.enPixelType;  //Pixel format of original image data
            stParam.nWidth        = stFrameInfo.nWidth;       //Image width
            stParam.nHeight       = stFrameInfo.nHeight;      //Image height
            stParam.nJpgQuality   = 70;						  //JPEG encoding quality

            //Target data
            stParam.enImageType   = MV_Image_Jpeg;            //Image type to be saved, convert to JPEG format
            stParam.nBufferSize   = nBufSize;                 //Storage node size
            unsigned char* pImage = (unsigned char*)malloc(nBufSize);
           stParam.pImageBuffer  = pImage;                   //Buffer for output data, this buffer is also used for storing converted picture data

            nRet = MV_CC_SaveImageEx2(m_handle, &stParam);
            if(MV_OK != nRet)
            {
                break;
            }

            //Save the converted picture data as file
             FILE* fp = fopen("image", "wb");
            fwrite(pImage, 1, stParam.nImageLen, fp);
            fclose(fp);
            free(pImage);

            //Process other image data...
            nTestFrameSize++;
        }
    }
    free(pFrameBuf);

    //Other process...

    //Stop image acquisition
     nRet = MV_CC_StopGrabbing(m_handle);
    if (MV_OK != nRet)
    {
        printf("error: StopGrabbing fail [%x]\n", nRet);
        return;
    }

    //Close camera
     nRet = MV_CC_CloseDevice(m_handle);
    if (MV_OK != nRet)
    {
        printf("error: CloseDevice fail [%x]\n", nRet);
        return;
    }

    //Destroy handle and release resources
     nRet = MV_CC_DestroyHandle(m_handle);
    if (MV_OK != nRet)
    {
        printf("error: DestroyHandle fail [%x]\n", nRet);
        return;
    }    
}